package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo6Window {

  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val ds: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = ds.flatMap(_.split(",")).map((_, 1))

    val ds2: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    //时间滚动窗口
    val ds3: WindowedStream[(String, Int), String, TimeWindow] = ds2.timeWindow(Time.seconds(5))

    val ds4: DataStream[(String, Int)] = ds3.sum(1)

    ds4.print()


    env.execute()

  }

}
